<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class BannerController extends CommonController {


	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$map['id'] = $sh_q;
			$map['name'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['addtime']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
		
		
		if(!empty(I('ban_type'))){
		$where['ban_type'] = I('ban_type');
		$shArray['ban_type'] = I('ban_type');
		}
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------
         	$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop_banner')->where($where)->order('id desc, add_time desc')->page($pageno.','.$pageCount)->select();
		foreach( $list as $key => $item){
		
		$list[$key]['author_name'] = M('administrator')->where('id="'.$item['author'].'"')->getField('username');
		
		}
		
		
		$this->assign('list',$list);// 赋值数据集
		$count = M('shop_banner')->where($where)->count();// 查询满足要求的总记录数
		$this->assign('count',$count);// 赋值分页输出	
		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->showAdmin();// 分页显示输出
		$typeArr  = array(1=>'主页',2=>'聚餐美食');
		$this->assign('typeArr', $typeArr);
	        $this->assign('page',$show);// 赋值分页输出	
        	$this->display('Banner/index');
    }
	
 public function editStatus(){
	         $cid = I('cid');
		 $status = I('state');
	         $dataInfo = M('shop_news')->find($cid);
		 
	         if( $dataInfo ){
                        if( $status == 1){
                          $data['status'] = 0;
                        }else{
                           $data['status'] = 1;
                        }
		   $result = M('shop_news')->where('id="'.$dataInfo['id'].'"')->save($data);
	         }
		 
	        if( $result ){
                        $info = 100;
                        $this->ajaxReturn($info);
		 }
	 
	 }
	 
  public  function edit(){
 
	  $id = I('id');
	  $dataInfo = M('shop_banner')->where('id="'.$id.'"')->find($id);
	  $typeArr  = array(1=>'主页',2=>'聚餐美食');
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->assign('typeArr', $typeArr);
		 $this->display();
	  }

	}
	
 public function del(){

            $id = $_REQUEST['id'];
			
            if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
			
            $list = M('shop_banner')->where($where)->delete();
			
		    $this->redirect('Banner/index');
		

    }


     public function add(){
		 $typeArr  = array(1=>'主页',2=>'聚餐美食');
		 $this->assign('typeArr', $typeArr);
		 $this->display();
    }
	
	
     public function save(){
            $picname="";
            $upload = new \Think\Upload();// 实例化上传类
		
            $upload->maxSize=3145728 ;// 设置附件上传大小
            $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  = './Uploads/bann/'; // 设置附件上传根目录
            $upload->savePath  =''; // 设置附件上传（子）目录
            // 上传文件 
            $upload->saveName = $_SESSION['admin_user_id'].time();
            $upload->autoSub = false;
	        $info = $upload->upload();
	        foreach($info as $file){        
                $picname =$file['savename'];
			    }
	        $data['add_time'] = time();
                $data['author'] = $_SESSION['admin_user_id'];
                $data['ban_pic'] = $picname;
                $data['name'] = I('name');
		$data['ban_type'] = I('ban_type');
				
		 if(M("shop_banner")->add($data)){
                    $this->redirect('Banner/index');
                }    
             
          }
		  
		  
        public function saveEdit(){
            $picname="";
            $upload = new \Think\Upload();// 实例化上传类
		
            $upload->maxSize=3145728 ;// 设置附件上传大小
            $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  = './Uploads/bann/'; // 设置附件上传根目录
            $upload->savePath  =''; // 设置附件上传（子）目录
            // 上传文件 
            $upload->saveName = $_SESSION['admin_user_id'].time();
            $upload->autoSub = false;
	    $info   =   $upload->upload();
	  
            foreach($info as $file){        
                $picname =$file['savename'];
	    }
	   $id = I('updateid');
	   if( !empty( $picname ) ){
			$data['ban_pic'] = $picname;
			$data['author'] = $_SESSION['admin_user_id'];
                        $data['name'] = I('name');
		        $data['ban_type'] = I('ban_type');
	   }else{
			$data['author'] = $_SESSION['admin_user_id'];
                        $data['name'] = I('name');
		        $data['ban_type'] = I('ban_type');
				
	   }
			
	  $result =  M("shop_banner")->where('id="'.$id.'"')->save($data);
	  $this->redirect('Banner/index');
          } 	
		 
}